package com.cy.util;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author: cy
 * @Date: 2022/01/01/15:15
 * @Description:
 */
public class CommunityUtil {


    //生成随机字符串
    public static String generateUUID(){
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 生成md5
     * @param key   明文加随机盐
     * @return  密文
     */
    public static String md5(String key){
        if(StrUtil.isBlank(key)){
            return null;
        }
        return DigestUtils.md5DigestAsHex(key.getBytes());
    }


    public static String getJsonString(Integer code, String msg, Map<String, Object> map){
        JSONObject json = new JSONObject();
        json.put("code", code);
        json.put("msg", msg);
        if(map != null){
            map.forEach((k, v) -> {
                json.put(k, v);
            });
        }
        return json.toJSONString();
    }

    public static String getJsonString(Integer code, String msg){

        return getJsonString(code, msg, null);
    }

    public static String getJsonString(Integer code){
        return getJsonString(code, null);
    }




}
